$(document).ready(function(){

    $("#fleche_gauche").click(function(){
        refresh_agenda("recule");
    });

    $("#fleche_droite").click(function(){
        refresh_agenda("avance");
    });
});

function refresh_agenda(action){
    $("#agenda").fadeOut("fast");
        id_user = $("#hidden_user").html();
        current_day = parseInt($("#hidden_day").text());
        current_month = parseInt($("#hidden_month").text());
        current_year = parseInt($("#hidden_year").text());
        current_page = $("#hidden_page").text();
        month="";
        year="";

        if(action=="recule"){
            if(current_month==1){
                new_month = 12;
                new_year = current_year-1;
            }else{
                month = current_month-1;
                if(month<10){
                    month="0"+month;
                }
                new_month = month;
                new_year = current_year;
            }
            new_date = new_month+"-"+new_year;

            $.ajax({
                type: 'POST',
                url: AJAX_PATH + 'agenda.ajax.php',
                data: "id="+id_user+"&day="+current_day+"&month="+new_month+"&year="+new_year+"&page="+current_page,
                success: function(html) {
                    $("#agenda").html(html);
                    $("#agenda").fadeIn("fast");
                }
            });
            $.ajax({
                type: 'POST',
                url: AJAX_PATH + 'getmois.ajax.php',
                data: "month="+new_month+"&year="+new_year,
                success: function(html) {
                    $("#mois_courant").html(html);
                }
            });
        }else if(action=="avance"){
            
            if(current_month==12){
                new_month = 01;
                new_year = current_year+1;
            }else{
                month = current_month+1;
                if(month<10){
                    month="0"+month;
                }
                new_month = month;
                new_year = current_year;
            }
            new_date = new_month+"-"+new_year;
            //alert("old: "+old_month+"-"+old_year+" new: "+new_date);
            $.ajax({
                type: 'POST',
                url: AJAX_PATH + 'agenda.ajax.php',
                data: "id="+id_user+"&day="+current_day+"&month="+new_month+"&year="+new_year+"&page="+current_page,
                success: function(html) {
                    $("#agenda").html(html);
                    $("#agenda").fadeIn("fast");
                }
            });
            $.ajax({
                type: 'POST',
                url: AJAX_PATH + 'getmois.ajax.php',
                data: "month="+new_month+"&year="+new_year,
                success: function(html) {
                    $("#mois_courant").html(html);
                }
            });
        }else{
            $.ajax({
                type: 'POST',
                url: AJAX_PATH + 'agenda.ajax.php',
                data: "id="+id_user+"&day="+current_day+"&month="+current_month+"&year="+current_year+"&page="+current_page,
                success: function(html) {
                    $("#agenda").html(html);
                    $("#agenda").fadeIn("fast");
                }
            });
            $.ajax({
                type: 'POST',
                url: AJAX_PATH + 'getmois.ajax.php',
                data: "month="+current_month+"&year="+current_year,
                success: function(html) {
                    $("#mois_courant").html(html);
                }
            });
        }

}